﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * MySQL后台任务数据库提供者。
 * 
 * 最后修改：2011-10-29
 * ************************************************************/
namespace Mozlite.Data.MySqlDataProvider
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mozlite.Common;
    using MySql.Data.MySqlClient;
    using System.Data;

    /// <summary>
    /// 后台任务数据库提供者。
    /// </summary>
    public class MySqlTaskDataProvider : TaskDataProvider
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Data.MySqlDataProvider.MySqlTaskDataProvider"/>。
        /// </summary>
        public MySqlTaskDataProvider() : base(MySqlClientFactory.Instance) { }


        public override void SetLastExecuteTime(string type, DateTime lastExecuteTime)
        {
            ExecuteNonQuery("REPLACE INTO {0}(`TaskType`,LastExecuteTime)VALUES('{1}', '{2}');", AddProfix("Tasks"), Q(type), Q(lastExecuteTime));
        }

        public override Dictionary<string, DateTime> LoadLastExecuteTimes()
        {
            Dictionary<string, DateTime> tasks = new Dictionary<string, DateTime>();
            using (IDataReader reader = ExecuteReader("SELECT * FROM {0};", AddProfix("Tasks")))
            {
                while (reader.Read()) {
                    tasks.Add(reader.GetString("TaskType"), reader.GetDateTime("LastExecuteTime"));
                }
                reader.Close();
            }
            return tasks;
        }
    }
}